package com.zhongan.gateway.common.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;

/**
 * 获取日期工具类
 * 
 * @author CHENQIMING
 */
public class DateUtil {

    public static final String      DATE_FORMAT_YYMMDD             = "yyMMdd";
    public static final String      DATE_FORMAT_YYMMDDHHMM         = "yyMMddHHmm";
    public static final String      DATE_FORMAT_YYMMDDHHMMSS       = "yyMMddHHmmss";
    public static final String      DATE_FORMATE_YYYY_MM_DD        = "yyyy-MM-dd";
    public static final String      DATE_FORMATE_YYYYMMDD          = "yyyyMMdd";
    public static final String      DATE_FORMATE_YYYY_MM_DD_HHMMSS = "yyyy-MM-dd HH:mm:ss";
    public static final String      DATE_FORMATE_YYYY_MM_DD_HHMM   = "yyyy-MM-dd HH:mm";
    public static final String      DATE_FORMATE_YYYYMMDD2         = "yyyy/MM/dd";

    private static final DateFormat ymdDateFormat                  = new SimpleDateFormat("yyMMdd");
    public static final DateFormat  ymdDateFormat2                 = new SimpleDateFormat(DATE_FORMATE_YYYY_MM_DD);
    private static final DateFormat ymdhmDateFormat                = new SimpleDateFormat("yyMMddHHmm");
    private static final DateFormat ymdhmsDateFormat               = new SimpleDateFormat("yyMMddHHmmss");
    private static final DateFormat hmsDateFormat                  = new SimpleDateFormat("HHmmss");
    public static final DateFormat  ymdhmDateFormat2               = new SimpleDateFormat(DATE_FORMATE_YYYY_MM_DD_HHMM);
    public static final DateFormat  mdhmDateFormat                 = new SimpleDateFormat("MM/dd  HH:mm");
    public static final DateFormat  mdDateFormat                   = new SimpleDateFormat("MM/dd");

    /**
     * 获取年月日，格式为yyMMdd
     * 
     * @return
     */
    public static String getYMD() {
        return ymdDateFormat.format(new Date());
    }

    public static String getYMD2() {
        return ymdDateFormat2.format(new Date());
    }

    /**
     * 获取年月日时分，格式为yyMMddHHmm
     * 
     * @return
     */
    public static String getYMDHM() {
        return ymdhmDateFormat.format(new Date());
    }

    public static Date string2date(String dateString, String formatStr) {
        if (StringUtils.isBlank(dateString)) {
            return null;
        }
        Date formateDate = null;
        DateFormat format = new SimpleDateFormat(formatStr);
        try {
            formateDate = format.parse(dateString);
        } catch (ParseException e) {
            return null;
        }
        return formateDate;
    }

    /**
     * 格式化日期
     * 
     * @param date 日期对象
     * @param formate 格式字符串
     * @return
     */
    public static String formatDate(Date date, String formate) {
        if (date == null) {
            return "";
        }
        SimpleDateFormat myFormat = new SimpleDateFormat(formate);
        return myFormat.format(date);
    }

    public static String formatDate(Long date, String formate) {
        if (date == null) {
            return "";
        }
        SimpleDateFormat myFormat = new SimpleDateFormat(formate);
        return myFormat.format(date);
    }

    /**
     * 获取年月日时分秒，格式为yyMMddHHmmss
     * 
     * @return
     */
    public static String getYMDHMS() {
        return ymdhmsDateFormat.format(new Date());
    }

    public static Date getNow() {
        return new Date();
    }

    public static long getNowTimestamp() {
        return getNow().getTime();
    }

    public static final Date addYears(Date aDate, int amount) {
        return addTime(aDate, 1, amount);
    }

    public static final Date addMonths(Date aDate, int amount) {
        return addTime(aDate, 2, amount);
    }

    public static final Date addDays(Date aDate, int amount) {
        return addTime(aDate, 6, amount);
    }

    public static final Date addHours(Date aDate, int amount) {
        return addTime(aDate, 10, amount);
    }

    public static final Date addMinutes(Date aDate, int amount) {
        return addTime(aDate, 12, amount);
    }

    public static final Date addSeconds(Date aDate, int amount) {
        return addTime(aDate, 13, amount);
    }

    private static final Date addTime(Date aDate, int timeType, int amount) {
        if (aDate == null) {
            return null;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(aDate);
        cal.add(timeType, amount);
        return cal.getTime();
    }

    /**
     * 使用参数Format将字符串转为Date
     */
    public static Date toDate(String strDate, String pattern) throws ParseException {
        return StringUtils.isBlank(strDate) ? null : new SimpleDateFormat(pattern).parse(strDate);
    }

    public static long getHHmmss(String source) {
        try {
            if (StringUtils.isEmpty(source)) {
                return hmsDateFormat.parse(source).getTime();
            } else {
                return hmsDateFormat.parse(hmsDateFormat.format(new Date())).getTime();
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public static long getYMD(Date source) {
        try {
            if (source != null) {
                return ymdDateFormat2.parse(ymdDateFormat2.format(source)).getTime();
            } else {
                return ymdDateFormat2.parse(ymdDateFormat2.format(new Date())).getTime();
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public static long getYMDByString(String source) {
        try {
            if (source != null) {
                return ymdDateFormat2.parse(source).getTime();
            } else {
                return ymdDateFormat2.parse(ymdDateFormat2.format(new Date())).getTime();
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * Date根据格式转成String
     * 
     * @param date
     * @param format
     * @return
     */
    public static String dateToString(Date date, String format) {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        String time = formatter.format(date);
        return time;
    }

    /**
     * 获取指定天的前一天
     * 
     * @param date
     * @return
     */
    public static Date getPreviousDate(Date date, String pattern) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int day = c.get(Calendar.DATE);
        c.set(Calendar.DATE, day - 1);

        String dayBefore = new SimpleDateFormat(pattern).format(c.getTime());
        try {
            Date previousDate = toDate(dayBefore, pattern);
            return previousDate;
        } catch (ParseException e) {
            return null;
        }

    }

    /**
     * 得到某年某月的第一天
     * 
     * @param yearMonth
     * @return
     */
    public static String getFirstDayOfMonth(String yearMonth) {
        int year = Integer.parseInt(yearMonth.split("-")[0]);
        int month = Integer.parseInt(yearMonth.split("-")[1]);
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month - 1);
        cal.set(Calendar.DAY_OF_MONTH, cal.getMinimum(Calendar.DATE));
        return ymdDateFormat2.format(cal.getTime());
    }

    /**
     * 得到某年某月的最后一天
     * 
     * @param yearMonth
     * @return
     */
    public static String getLastDayOfMonth(String yearMonth) {
        int year = Integer.parseInt(yearMonth.split("-")[0]);
        int month = Integer.parseInt(yearMonth.split("-")[1]);
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month - 1);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        int value = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, value);
        return ymdDateFormat2.format(cal.getTime());
    }

    /**
     * 获取当天的开始时间和结束时间 flag为0时则为开始时间，为1时则为结束时间
     * 
     * @param flag
     * @return
     */
    public static Date getBeginOrEndDayTime(int flag, Date date) {
        if (date == null) {
            date = new Date();// 取时间
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int hour = cal.get(Calendar.HOUR_OF_DAY);
        int minute = cal.get(Calendar.MINUTE);
        int second = cal.get(Calendar.SECOND);
        // 时分秒（毫秒数）
        long millisecond = hour * 60 * 60 * 1000 + minute * 60 * 1000 + second * 1000;
        // 凌晨00:00:00
        cal.setTimeInMillis(cal.getTimeInMillis() - millisecond);

        if (flag == 0) {
            return cal.getTime();
        } else if (flag == 1) {
            // 凌晨23:59:59
            cal.setTimeInMillis(cal.getTimeInMillis() + 23 * 60 * 60 * 1000 + 59 * 60 * 1000 + 59 * 1000);
        }
        return cal.getTime();
    }
}
